<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_nongfee/model')->方法名();
*/
namespace jy_nongfee;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function up_task($cangid=0){
		if(!$_SESSION['uniacid']){
			return false;
		}
		//$this->up_cang();
		if($cangid){
			$cang=pdo_get('jy_nongfee_cang',array('id'=>$cangid,'uniacid'=>$_SESSION['uniacid']));
			if($cang['status']){
				return false;
			}
			$cangs[0]=$cang;
		}else{
			$cangs=pdo_getall('jy_nongfee_cang',array('status'=>0,'uniacid'=>$_SESSION['uniacid']));
		}
		foreach($cangs as $cv){
			$yaos=pdo_getall('jy_nongfee_yao',array('cangid'=>$cv['id'],'uniacid'=>$_SESSION['uniacid']));
			foreach($yaos as $v){
				$this->up_yao($v);
				if(!$v['status'] && $v['startime']<=TIMESTAMP){
					$this->start_yao($v);
				}
			}
		}
	}
	public function up_cang(){
		if(!$this->setting['open_cang']){
			return false;
		}
		$item=pdo_get('jy_nongfee_cang',array('status'=>0,'uniacid'=>$_SESSION['uniacid']));
		if(!$item['id']){
			$paixu=pdo_count('jy_nongfee_cang',array('uniacid'=>$_SESSION['uniacid']));
			$data=array(
						'uniacid'=>$_SESSION['uniacid'],
						'allget_rate'=>$this->setting['allget_rate']?:30,
						'jian_num'=>$this->setting['jian_num']?:3,
						'jian_jinum'=>$this->setting['jian_jinum']?:3000,
						'jian_jinrate'=>$this->setting['jian_jinrate']?:33,
						'jian_jinmin'=>$this->setting['jian_jinmin']?:1,
						'jian_jinmax'=>$this->setting['jian_jinmax']?:100,
						'jian_upjinmax'=>$this->setting['jian_upjinmax']?:100,
						'jian_cannum'=>$this->setting['jian_cannum']?:3,
						'jian_getrate'=>$this->setting['jian_getrate']?:10,
						'jian_tonum'=>$this->setting['jian_tonum']?:2,
						'jian_tobi'=>$this->setting['jian_tobi']?:50,
						'jian_tofee'=>$this->setting['jian_tofee']?:50,
						'paixu'=>$paixu+1,
						'createtime'=>TIMESTAMP,
						'status'=>0,
			);
			$this->insert('cang',$data);
			$data['id']=pdo_insertid();
			$this->add_yao($data);
		}
		return true;
	}
	public function add_yao($item){
		if($item['status']){
			return false;
		}
		$nownum=pdo_count('jy_nongfee_yao',array('status <='=>1,'cangid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
		$yaonum=pdo_count('jy_nongfee_yao',array('cangid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
		if($yaonum<$item['jian_num'] && $nownum<=2){
			$is7=pdo_get('jy_nongfee_yao',array('status'=>7,'cangid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			if($is7['id']){
				return false;
			}
			$jian_days=0;
			$jian_jinrate=$item['jian_jinrate'];
			$jian_upjinmax=$item['jian_upjinmax'];
			$jian_days=$this->setting['jian_days']?:3;
			if($item['jian_num']-$yaonum>3){
				$jian_num=$yaonum+3;
			}else{
				$jian_num=$item['jian_num'];
			}
			$lastyao=pdo_get('jy_nongfee_yao',array('cangid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'','id desc');
			$startime=$lastyao['endtime']?:TIMESTAMP;
			$day=date('Y-m-d',$startime);
			$startime=strtotime($day.' '.$item['jian_dayhour']);
			$jian_jinum=$lastyao['alljifee']?:$item['jian_jinum'];
			$jian_jinmax=$lastyao['jian_jinmax']?:$item['jian_jinmax'];
			for($i=$yaonum;$i<$jian_num;$i++){
				$startime +=$jian_daystime;
				if($i){
					$jian_jinum=intval((100+$jian_jinrate)*$jian_jinum/100);
					$jian_jinmax=$jian_jinmax+$jian_upjinmax;
				}
				$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'cangid'=>$item['id'],
					'alljifee'=>$jian_jinum,
					'jian_jinmax'=>$jian_jinmax,
					'paixu'=>$i+1,
					'createtime'=>TIMESTAMP,
					'startime'=>$startime,
					'endtime'=>$startime+$jian_days*24*3600,
					'status'=>$i?0:1,
				);
				$this->insert('yao',$data);
				if(!$i){
					$yaoid=$this->insertid();
					$this->model('ai')->up_canlog($yaoid);
					$this->model('ai')->up_yuelog($yaoid);
				}
				$jian_daystime=$jian_days*24*3600;
			}
		}
		return true;
	}
	public function up_yao($yao){
		if($yao['status']!=1){
			return false;
		}
		if($yao['endtime']<=TIMESTAMP){
			$this->up_outtime($yao);
		}
		return true;
	}
	public function start_yao($yao){
		//刷新状态；
		$yao['status']=pdo_getcolumn('jy_nongfee_yao',array('id'=>$yao['id']),'status');
		if($yao['status']){
			return $yao['status'];
		}
		if(!$yao['status'] && $yao['startime']<=TIMESTAMP){
			pdo_update('jy_nongfee_yao',array('status'=>1),array('id'=>$yao['id'],'uniacid'=>$_SESSION['uniacid']));
			$this->model('ai')->up_canlog($yao['id']);
			$this->model('ai')->up_yuelog($yao['id']);
			$yao['status']=1;
		}
		return $yao['status'];
	}
	public function up_outtime($yao=array(),$jian_getrate=0){
		if($yao['endtime']>TIMESTAMP){
			return false;
		}
		//未开始的停种
		pdo_update('jy_nongfee_yao',array('status'=>7,'successtime'=>TIMESTAMP),array('status'=>0,'cangid'=>$yao['cangid'],'uniacid'=>$_SESSION['uniacid']));
		pdo_update('jy_nongfee_yao',array('status'=>3,'successtime'=>TIMESTAMP),array('id'=>$yao['id'],'uniacid'=>$_SESSION['uniacid']));
		pdo_update('jy_nongfee_cang',array('status'=>2,'successtime'=>TIMESTAMP),array('id'=>$yao['cangid'],'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	public function up_sifang($cang=array(),$isqiang=false){
		if($cang['status']!=2 && $cang['status']!=3 && !$isqiang){
			exi('释放失败,该庄园不支持手动平仓！','error');
		}
		$manyaos=pdo_count('jy_nongfee_yao',array('cangid'=>$cang['id'],'status'=>array(2,5),'uniacid'=>$_SESSION['uniacid']));
		$status=array(2,3,4);
		$params=array('cangid'=>$cang['id'],'status'=>$status,'uniacid'=>$_SESSION['uniacid']);
		$yaos=pdo_getall('jy_nongfee_yao',$params);
		$num=pdo_count('jy_nongfee_cang',array('id <='=>$cang['id'],'uniacid'=>$_SESSION['uniacid']));
		$cangtitle=$cang['title'];
		foreach($yaos as $yao){
					//未满期是否进入奖金池
					if($yao['status']==3 && $cang['pingc_lastistofee']){
						$this->up_tofeerate($yao,$cang);
					}
					$pingc_tobi=$cang['pingc_tobi'];
					$pingc_tofee=$cang['pingc_tofee'];
					$pingc_toguo=$cang['pingc_toguo'];
					$title=$yao['status']==3?'未建满释放':'收益封顶平仓释放';
					$yaotitle=$yao['paixu']?'第【'.$yao['paixu'].'】期':'';
					$title=$cangtitle.$yaotitle.$title;
					$canlogs=pdo_getall('jy_nongfee_canlog',array('yaoid'=>$yao['id'],'status'=>array(0,1,2),'uniacid'=>$_SESSION['uniacid']));
					$feelog=array(
						'cangid'=>$yao['cangid'],
						'yaoid'=>$yao['id'],
					);
					foreach($canlogs as $cv){
						$getfee=$cv['jifen'];
						$feelog['mid']=$cv['mid'];
						$feelog['canlogid']=$cv['id'];
						if($yao['status']!=3){
							if($pingc_tobi){
								$fee=$pingc_tobi*$getfee/100;
								model('core/member')->member_credit($cv['mid'],'credit4',$fee,true,'jy_nongfee',$title);
								$feelog['fee']=$fee;
								$this->add_feelog($feelog,2);
							}
							if($pingc_tofee){
								$fee=$pingc_tofee*$getfee/100;
								model('core/member')->member_credit($cv['mid'],'credit1',$fee,true,'jy_nongfee',$title);
								$feelog['fee']=$fee;
								$this->add_feelog($feelog,3);
							}
							if($pingc_toguo){
								$fee=$pingc_toguo*$getfee/100;
								pdo_update('jy_nongfee_member',array('baofee +='=>$fee),array('mid'=>$cv['mid'],'uniacid'=>$_SESSION['uniacid']));
								$feelog['fee']=$fee;
								$this->add_feelog($feelog,7);
							}
						}else{
							model('core/member')->member_credit($cv['mid'],'credit4',$getfee,true,'jy_nongfee',$cangtitle.$yaotitle.''.$this->cd_title.'返回');
							$feelog['fee']=$getfee;
							$this->add_feelog($feelog,12);
						}
					}
					pdo_update('jy_nongfee_canlog',array('status'=>3),array('yaoid'=>$yao['id'],'status'=>array(0,1),'uniacid'=>$_SESSION['uniacid']));
					pdo_update('jy_nongfee_yao',array('status'=>6),array('id'=>$yao['id'],'uniacid'=>$_SESSION['uniacid']));
		}
		pdo_update('jy_nongfee_cang',array('status'=>4),array('id'=>$cang['id'],'uniacid'=>$_SESSION['uniacid']));
		//释放奖金池
		$cang['status']=4;
		$this->up_redfeetouser($cang);
		return true;
	}
	public function up_success($yaoid){
		if(!$yaoid){
			return false;
		}
		$yao=pdo_get('jy_nongfee_yao',array('id'=>$yaoid,'uniacid'=>$_SESSION['uniacid']),'','createtime desc');
		if(!$yao['id']){
			return false;
		}
		$isfee=pdo_getsum('jy_nongfee_canlog',array('yaoid'=>$yaoid,'uniacid'=>$_SESSION['uniacid']),'jifen');
		$cang=pdo_get('jy_nongfee_cang',array('id'=>$yao['cangid'],'uniacid'=>$_SESSION['uniacid']));
		$jian_getrate=$cang['jian_getrate'];
		if($isfee>=$yao['alljifee']){
			pdo_update('jy_nongfee_yao',array('status'=>2,'successtime'=>TIMESTAMP),array('id'=>$yao['id'],'uniacid'=>$_SESSION['uniacid']));
			//每期种植'.$this->cd_title.'总数量进入奖金池
			$this->up_tofeerate($yao,$cang);
			pdo_update('jy_nongfee_canlog',array('status'=>1),array('yaoid'=>$yao['id'],'uniacid'=>$_SESSION['uniacid']));
			$num=pdo_count('jy_nongfee_yao',array('status'=>2,'cangid'=>$yao['cangid'],'uniacid'=>$_SESSION['uniacid']));
			if($num>=$cang['jian_tonum']){
				$isyao=pdo_get('jy_nongfee_yao',array('status'=>2,'cangid'=>$yao['cangid'],'uniacid'=>$_SESSION['uniacid']),'','id asc');
				if($isyao['id']){
					pdo_update('jy_nongfee_yao',array('status'=>5),array('id'=>$isyao['id'],'uniacid'=>$_SESSION['uniacid']));
					$canlogs=pdo_getall('jy_nongfee_canlog',array('status'=>1,'yaoid'=>$isyao['id'],'uniacid'=>$_SESSION['uniacid']));
					pdo_update('jy_nongfee_canlog',array('status'=>2),array('yaoid'=>$isyao['id'],'uniacid'=>$_SESSION['uniacid']));
					file_put_contents(ROOT_D.'/up_success.txt',time()."\n",FILE_APPEND);
					foreach($canlogs as $cv){
						$this->up_tofee($cv,$jian_getrate);
					}
					file_put_contents(ROOT_D.'/up_success.txt',time()."\n",FILE_APPEND);
				}
			}
			$isnum=pdo_count('jy_nongfee_yao',array('status >'=>1,'cangid'=>$yao['cangid'],'uniacid'=>$_SESSION['uniacid']));
			if($isnum>=$cang['jian_num']){
				pdo_update('jy_nongfee_cang',array('status'=>1),array('id'=>$yao['cangid'],'uniacid'=>$_SESSION['uniacid']));
			}
			return true;
		}else{
			return false;
		}
	}
	public function up_tofee($canlog,$jian_getrate=0){
		if(!$canlog || !$jian_getrate){
			return false;
		}
		$idadd=pdo_getcolumn('jy_nongfee_feelog',array('type'=>0,'canlogid'=>$canlog['id'],'yaoid'=>$canlog['yaoid'],'cangid'=>$canlog['cangid'],'uniacid'=>$_SESSION['uniacid']),'id');
		if($idadd){
			return false;
		}
		$getfee=$canlog['jifen']*$jian_getrate/100;
		if($canlog['baobei']>0){
			$getfee=$getfee*$canlog['baobei'];
			$yaotitle.=$canlog['baobei'].'倍';
		}
		$credit4=$canlog['jifen']-$getfee;
		$cangtitle=pdo_getcolumn('jy_nongfee_cang',array('id'=>$canlog['cangid'],'uniacid'=>$_SESSION['uniacid']),'title');
		$yao=pdo_get('jy_nongfee_yao',array('id'=>$canlog['yaoid'],'uniacid'=>$_SESSION['uniacid']));
		$yaotitle='第【'.$yao['paixu'].'】期';
		model('core/member')->member_credit($canlog['mid'],'credit3',$getfee,true,'jy_nongfee',$cangtitle.$yaotitle.'种植收益');
		model('core/member')->member_credit($canlog['mid'],'credit4',$credit4,true,'jy_nongfee',$cangtitle.$yaotitle.'种植收益释放');
		$canlog['getfee']=$getfee;
		$canlog['fee']=$getfee;
		$this->add_feelog($canlog,0);
		$this->add_jilog($canlog,0);
		//$this->model('team')->add_commission($canlog,$cangtitle,$yaotitle);
		//$this->model('commission')->create_area_rate($canlog,$cangtitle,$yaotitle);
		return true;
	}
	public function up_canginfo($item){
		$day=date('Y-m-d',$item['createtime']);
		$startime=strtotime($day);
		$endtime=$startime+24*3600;;
		$num=pdo_count('jy_nongfee_cang',array('id <='=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
		$item['isyaonum']=pdo_count('jy_nongfee_yao',array('cangid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'status'=>array(2,5)));
		$now=pdo_get('jy_nongfee_yao',array('cangid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'status'=>1));
		if(!$now['id']){
			$now=pdo_get('jy_nongfee_yao',array('cangid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'status'=>0),'','id asc');
		}
		$item['nowyaonum']=pdo_count('jy_nongfee_yao',array('id <='=>$now['id'],'cangid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
		$item['title']=$item['title']?:'第【'.$num.'】庄园';
		$item['desc']='已建好【'.$item['isyaonum'].'】'.$this->y_title.'，第'.$item['nowyaonum'].''.$this->y_title.'种植中，最低可投入（'.$item['jian_jinmin'].'）'.$this->cd_title.'，每次投入'.$this->cd_title.'种植收益'.$item['jian_getrate'].'%';
		return $item;
	}
	//每期种植'.$this->cd_title.'总数量进入奖金池
	public function up_tofeerate($yao,$cang=array()){
		$cang=$cang?:pdo_get('jy_nongfee_cang',array('id'=>$yao['cangid'],'uniacid'=>$_SESSION['uniacid']));
		if(!$cang['pingc_tofeerate']){
			return false;
		}
		$isfee=pdo_getsum('jy_nongfee_canlog',array('yaoid'=>$yao['id'],'cangid'=>$yao['cangid'],'uniacid'=>$_SESSION['uniacid']),'jifen');
		$tofee=$cang['pingc_tofeerate']*$isfee/100;
		pdo_update('jy_nongfee_cang',array('redfee +='=>$tofee),array('id'=>$yao['cangid'],'uniacid'=>$_SESSION['uniacid']));
		$yao['fee']=$tofee;
		$this->add_redfeelog($yao);
		return true;
	}
	//本场种植庄园'.$this->cd_title.'奖金池总数量÷本场最后一期种植'.$this->cd_title.'总数量=每个'.$this->cd_title.'奖金值；每个'.$this->cd_title.'奖金值×本场最后一期每笔种植'.$this->cd_title.'的数量=获得每笔种植'.$this->cd_title.'奖金；
	public function up_redfeetouser($cang=array(),$isshou=false){
		if(!$cang['pingc_nowlastistofee'] && !$isshou){
			return false;
		}
		if($cang['status']<3){
			return false;
		}
		$redfee=pdo_getsum('jy_nongfee_redfeelog',array('status'=>0,'cangid'=>$cang['id'],'uniacid'=>$_SESSION['uniacid']),'fee');
		if($redfee<0){
			return false;
		}
		$canlog=pdo_get('jy_nongfee_canlog',array('cangid'=>$cang['id'],'uniacid'=>$_SESSION['uniacid']),'yaoid','id desc');
		$yaoid=$canlog['yaoid'];
		$lastcanfee=pdo_getsum('jy_nongfee_canlog',array('yaoid'=>$yaoid,'cangid'=>$cang['id'],'uniacid'=>$_SESSION['uniacid']),'jifen');
		if(!$lastcanfee){
			return false;
		}
		$paixu=pdo_getcolumn('jy_nongfee_yao',array('id'=>$yaoid),'paixu');
		$title=$cang['title'].'第'.$paixu.'期奖金池释放';
		$onedou=$redfee/$lastcanfee;
		$canlogs=pdo_getall('jy_nongfee_canlog',array('yaoid'=>$yaoid,'cangid'=>$cang['id'],'uniacid'=>$_SESSION['uniacid']));
		foreach($canlogs as $cv){
			$fee=$onedou*$cv['jifen'];
			model('core/member')->member_credit($cv['mid'],'credit4',$fee,true,'jy_nongfee',$title);
			$cv['fee']=$fee;
			$this->add_feelog($cv,16);
		}
		pdo_update('jy_nongfee_redfeelog',array('status'=>1),array('status'=>0,'cangid'=>$cang['id'],'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	public function get_cangtitle($cangid){
		if(!$cangid){
			return '';
		}
		$cangtitle=pdo_getcolumn('jy_nongfee_cang',array('id'=>$cangid,'uniacid'=>$_SESSION['uniacid']),'title');
		return $cangtitle;
	}
	public function team_level_up_types(){
		return str_array($this->teamsetting['level_up_types']);
	}
	public function current_level_up_types(){
		return str_array($this->areasetting['level_up_types']);
	}
	public function add_redfeelog($item,$type=0){
		$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'cangid'=>$item['cangid'],
					'yaoid'=>$item['yaoid'],
					'fee'=>$item['fee'],
					'type'=>$type,
					'createtime'=>TIMESTAMP,
		);
		$this->insert('redfeelog',$data);
		return true;
	}
	public function add_feelog($item,$type=0){
		$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$item['mid']?:$_SESSION['uid'],
					'cangid'=>$item['cangid'],
					'yaoid'=>$item['yaoid'],
					'canlogid'=>$item['canlogid']?:$item['id'],
					'yuelogid'=>$item['yuelogid'],
					'fee'=>$item['fee'],
					'type'=>$type,
					'createtime'=>TIMESTAMP,
					'status'=>0,
		);
		$this->insert('feelog',$data);
		return true;
	}
	public function add_jilog($item,$type=0){
		$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$item['mid']?:$_SESSION['uid'],
					'cangid'=>$item['cangid'],
					'yaoid'=>$item['yaoid'],
					'canlogid'=>$item['canlogid']?:$item['id'],
					'jifen'=>$item['fee'],
					'type'=>$type,
					'createtime'=>TIMESTAMP,
					'status'=>0,
		);
		$this->insert('jilog',$data);
		return true;
	}
	public function add_baolog($item,$type=0){
		$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$item['mid']?:$_SESSION['uid'],
					'cangid'=>$item['cangid'],
					'yaoid'=>$item['yaoid'],
					'canlogid'=>$item['canlogid']?:$item['id'],
					'jifen'=>$item['jifen'],
					'createtime'=>TIMESTAMP,
		);
		$this->insert('baolog',$data);
		return true;
	}
	public function up_guo2dou($item){
		$cang=pdo_get('jy_nongfee_cang',array('id'=>$item['cangid'],'uniacid'=>$_SESSION['uniacid']));
		if($cang['jian_guo2dou'] && $item['jifen']>0){
			$fee=$cang['jian_guo2dou']*$item['jifen']/100;
			$member=pdo_get('jy_nongfee_member',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),'baofee');
			if($member['baofee'] >=$fee){
				pdo_update('jy_nongfee_member',array('baofee -='=>$fee),array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));
				model('core/member')->member_credit($item['mid'],'credit4',$fee,true,'jy_nongfee','茶果兑换'.$this->cd_title.'');
				$item['fee']=$fee;
				$this->add_feelog($item,8);
			}
		}
		return true;
	}
	public function up_canlog($item,$jian_xiaojinum=0,$credit1num=0,$bei=0){
		$status=pdo_getcolumn('jy_nongfee_yao',array('id'=>$item['yaoid'],'uniacid'=>$_SESSION['uniacid']),'status');
		if($status==2){
			exi('种植失败！该期已满！','error');
		}
		$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$_SESSION['uid'],
					'cangid'=>$item['cangid'],
					'yaoid'=>$item['yaoid'],
					'jifen'=>$item['jifen'],
					'baobei'=>$bei,
					'dayid'=>date('Y-m-d'),
					'createtime'=>TIMESTAMP,
					'status'=>0,
		);
		$this->insert('canlog',$data);
		$canlogid=pdo_insertid();
		model('core/member')->member_credit($_SESSION['uid'],'credit4',$item['jifen'],false,'jy_nongfee','参与种植');
		if($jian_xiaojinum>0){
			model('core/member')->member_credit($_SESSION['uid'],'credit1',$jian_xiaojinum,false,'jy_nongfee','参与种植消耗积分');
		}
		if($credit1num>0){
			model('core/member')->member_credit($_SESSION['uid'],'credit1',$credit1num,false,'jy_nongfee','购买'.$bei.'倍的加倍包','duibao');
		}
		$this->up_success($item['yaoid']);
		$data['fee']=$data['jifen'];
		$data['canlogid']=$canlogid;
		$this->add_feelog($data,4);
		$this->up_guo2dou($data);
		$data['id']=$canlogid;
		$this->model('commission')->create_area_rate($data);
		return true;
	}
	public function up_yuelog($item,$jian_xiaojinum=0,$credit1num=0,$bei=0){
		$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$_SESSION['uid'],
					'cangid'=>$item['cangid'],
					'yaoid'=>$item['yaoid'],
					'jifen'=>$item['jifen'],
					'baobei'=>$bei,
					'type'=>$item['type'],
					'dayid'=>date('Y-m-d'),
					'createtime'=>TIMESTAMP,
					'status'=>0,
		);
		$this->insert('yuelog',$data);
		$yuelogid=pdo_insertid();
		pdo_update('jy_nongfee_member',array('zquan -='=>1),array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		$quanlog=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'cangid'=>$item['cangid'],
			'yaoid'=>$item['yaoid'],
			'num'=>1,
			'type'=>1,
			'createtime'=>TIMESTAMP,
		);
		$this->insert('quanlog',$quanlog);
		model('core/member')->member_credit($_SESSION['uid'],'credit4',$item['jifen'],false,'jy_nongfee','预约种植');
		$allyueifen=$jian_xiaojinum-$credit1num;
		if($allyueifen>0){
			model('core/member')->member_credit($_SESSION['uid'],'credit1',$allyueifen,false,'jy_nongfee','预约种植消耗积分');
		}
		if($credit1num>0){
			model('core/member')->member_credit($_SESSION['uid'],'credit1',$credit1num,false,'jy_nongfee','购买'.$bei.'倍的加倍包','duibao');
		}
		$data['fee']=$data['jifen'];
		$data['yuelogid']=$yuelogid;
		$this->add_feelog($data,14);
		return true;
	}
	public function check_buy($yao,$fee=0,$isyue=0,$type=0){
		if(!$isyue){
			$iscan=pdo_count('jy_nongfee_canlog',array('mid'=>$_SESSION['uid'],'yaoid'=>$yao['id'],'uniacid'=>$_SESSION['uniacid'],'dayid'=>date('Y-m-d')),'id');
			$team=pdo_get('jy_nongfee_cang',array('uniacid'=>$_SESSION['uniacid'],'id'=>$yao['cangid']));
			if($iscan>=$team['jian_cannum'] && $this->setting['jian_mid']!=$_SESSION['uid']){
				exi('您今天已参与'.$iscan.'次，请明天再来吧！','error');
			}
		}else{
			$quan_hour=$this->setting['quan_hour'];
			$quan_yuehour=$this->setting['quan_yuehour'];
			$quan_hour=$type?$quan_hour:$quan_yuehour;
			if($quan_hour && $yao['startime']-TIMESTAMP<$quan_hour*3600){
				exi('至少提前'.$quan_hour.'小时，才能预约种植！','error');
			}
			$isyuejifen=pdo_getsum('jy_nongfee_yuelog',array('yaoid'=>$yao['id'],'uniacid'=>$_SESSION['uniacid']),'jifen');
			$quan_rate=$this->setting['quan_rate'];
			if($isyuejifen && $quan_rate){
				$israte=$isyuejifen*100/$yao['alljifee'];
				if($israte>=$quan_rate){
					exi('预约种植已经超过，该期最大比例'.$quan_rate.'%，该期不能再预约了！','error');
				}
				$newjifen=$isyuejifen+$fee;
				$isnewrate=$newjifen*100/$yao['alljifee'];
				if($isnewrate>=$quan_rate){
					$canyuefee=$yao['alljifee']*$quan_rate/100;
					$lastfee=$canyuefee-$isyuejifen;
					exi('当前剩余可预约额度'.$lastfee.'个！','error');
				}
			}
			$istimes=pdo_count('jy_nongfee_yuelog',array('mid'=>$_SESSION['uid'],'yaoid'=>$yao['id'],'uniacid'=>$_SESSION['uniacid']),'id');
			$quan_times=$this->setting['quan_times'];
			if($quan_times && $istimes>=$quan_times){
				exi('最多可以预约种植'.$quan_times.'次，该期不能再预约了！','error');
			}
		}
		$isfee=pdo_getsum('jy_nongfee_canlog',array('yaoid'=>$yao['id'],'uniacid'=>$_SESSION['uniacid']),'jifen');
		$lastfee=$yao['alljifee']-$isfee;
		if($fee>$lastfee){
			exi('当前剩余'.$lastfee.'可投入'.$this->cd_title.'！','error');
		}
		return true;
	}
}
?>